package com.example.materialtest

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import java.util.*


class MainActivity : AppCompatActivity(){
    lateinit var drawerlayout : DrawerLayout
    private var mTabRadioGroup: RadioGroup? = null
    private lateinit var mViewPager: ViewPager
    private var mFragments: List<Fragment>? = null
    private var mAdapter: FragmentPagerAdapter? = null
    /*val vp = LayoutInflater.from(this).inflate(R.layout.images_first, null).findViewById<View>(R.id.main_vp) as ViewPager?
    val ll = LayoutInflater.from(this).inflate(R.layout.images_first, null).findViewById<View>(R.id.main_ll) as LinearLayout*/
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        val tool=findViewById<Toolbar>(R.id.Tool)
        setSupportActionBar(tool)
        drawerlayout = findViewById(R.id.drawerlayout)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.cebian)
        }
        drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
    }


    private fun initView() {
        mViewPager = findViewById(R.id.viewpager)
        mTabRadioGroup = findViewById(R.id.dibumenu)
        mFragments = java.util.ArrayList<Fragment>(3)
        (mFragments as java.util.ArrayList<Fragment>).add(MessFragment.newInstance()!!)
        (mFragments as java.util.ArrayList<Fragment>).add(FirstFragment.newInstance()!!)
        (mFragments as java.util.ArrayList<Fragment>).add(MineFragment.newInstance()!!)
        mAdapter = MyFragmentPagerAdapter(supportFragmentManager, mFragments)
        mViewPager.adapter = mAdapter
        mViewPager.addOnPageChangeListener(mPageChangeListener)
        mTabRadioGroup?.setOnCheckedChangeListener(mOnCheckedChangeListener)
    }

    override fun onDestroy() {
        super.onDestroy()
        mViewPager.removeOnPageChangeListener(mPageChangeListener)
    }

    private val mPageChangeListener: ViewPager.OnPageChangeListener = object : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
        override fun onPageSelected(position: Int) {
            val radioButton = mTabRadioGroup!!.getChildAt(position) as RadioButton
            radioButton.isChecked = true

        }

        override fun onPageScrollStateChanged(state: Int) {}
    }

    private val mOnCheckedChangeListener =
        RadioGroup.OnCheckedChangeListener { group, checkedId ->
            for (i in 0 until group.childCount) {
                if (group.getChildAt(i).id == checkedId) {
                    /*val fristpg=findViewById<FrameLayout>(R.id.fristpg)
                    val minepg=findViewById<FrameLayout>(R.id.minepg)
                    val messagepg=findViewById<FrameLayout>(R.id.messagepg)*/
                    val tool=findViewById<Toolbar>(R.id.Tool)
                    //var naviView=findViewById<NavigationView>(R.id.naviView)
                    mViewPager.currentItem = i
                    if (i==0){
                        tool.visibility=View.INVISIBLE
                        //naviView.visibility=View.VISIBLE
                    }else if(i==1){
                        tool.visibility=View.VISIBLE
                        //naviView.visibility=View.GONE
                    }else{
                        tool.visibility=View.INVISIBLE
                    }
                    /*if(i%3==0){
                        messagepg.visibility=View.VISIBLE
                        fristpg.visibility=View.INVISIBLE
                        minepg.visibility=View.INVISIBLE
                    }else if(i%3==1){
                        messagepg.visibility=View.INVISIBLE
                        fristpg.visibility=View.VISIBLE
                        minepg.visibility=View.INVISIBLE

                    }else{
                        messagepg.visibility=View.INVISIBLE
                        fristpg.visibility=View.INVISIBLE
                        minepg.visibility=View.VISIBLE
                    }*/
                    return@OnCheckedChangeListener
                }
            }
        }

    private class MyFragmentPagerAdapter(fm: FragmentManager?, private val mList: List<Fragment>?) : FragmentPagerAdapter(fm!!) {
        override fun getCount(): Int {
            return if (mList == null) 0 else mList.size
        }

        override fun getItem(position: Int): Fragment {
            return mList?.get(position)!!
        }
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        //val button_ret=findViewById<ImageButton>(R.id.button_ret)
        when(item.itemId){
            android.R.id.home -> {
                drawerlayout.openDrawer(GravityCompat.END)
                //button_ret.visibility=View.VISIBLE
            }
        }
        return true
    }


}